<?php
/**
 * Created by JetBrains PhpStorm.
 * File: RoleAction.class.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-11
 * Time: 下午10:30
 */

	class MenuAction extends CommonAction{

		//菜单列表
		function index(){

			$menu = D('Menu');
			import("ORG.Util.page");

			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					$datas = array();
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
					}
					$list = $menu->relation(true)->where($datas)->order('id desc')->select();
					$count = $menu->where($datas)->count('id');
					foreach($list as $key=>$value){
						$list[$key]['count'] = count($list[$key]['menuitem']);
					}
					if($count<1){
						//	$this->assign('isshow', 'no');
						$this->error('没有该菜单数据');
					}elseif($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));

					$show = $page->fpage($pagedata);
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');

				}

			}else{
				$count = $menu->count();
				if($count<C('PAGENUM')){
					$pagedata = array(1);
				}else{
					$pagedata = array(0,1,2,3,4,5,7,8);
				}
				$page = new page($count, C('PAGENUM'));
				$show = $page->fpage($pagedata);
				$list = $menu->relation(true)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
				foreach($list as $key=>$value){
					$list[$key]['count'] = count($list[$key]['menuitem']);
				}
				if($count==0){
					$this->assign('isshow', 'no');
					$this->assign('nobody', 'no');
				}else{
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');
				}
			}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display('index');
		}

		//指向添加菜单界面
		function add(){
			$this->display('add');
		}

		//添加菜单
		function insert(){
			$menu = D('Menu');
			if($menu->create()){
				if($menu->add()){
					$this->success('添加菜单成功', __APP__.'/Menu/index');
				}else{
					$this->error('添加菜单失败');
				}
			}else{
				$this->error($menu->getError());
			}
		}

		//删除分组
		function del(){

			if(!empty($_POST['del'])){
				$menu = D('Menu');
				foreach($_POST['del'] as $key=>$value){
					$list = $menu->relation(true)->where('id='.$value)->select();
					if(count($list[0]['menuitem'])>0){
						$this->error('[ '.$list[0]['name'].' ] 菜单下还有菜单项');
					}
					else{
						$num = $menu->delete($value);
					}
					if($num>0){
						$this->success('删除菜单成功');
					}else{
						$this->error('删除菜单失败');
					}
				}
			}else{
				$this->error('请选择菜单');
			}
		}

		//指向修改分组界面
		function edit(){
			if($_GET['id'] && !empty($_GET['id'])){
				$menu = M('Menu');
				$list = $menu->find($_GET['id']);
				$this->assign('list', $list);
				$this->display();
			}else{
				$this->error('异常操作');
			}
		}

		//修改分组
		function update(){
			if(isset($_POST['id']) && !empty($_POST['id'])){
				$menu = D('Menu');
						if($menu->create()){
							if($menu->save()){
								$this->success('修改菜单成功', __APP__.'/Menu/index');
							}else{
								$this->error('修改菜单失败');
							}
						}else{
							$this->error($menu->getError());
						}
			}else{
				$this->error('异常操作');
			}
		}

	}